/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package main.genomemap;

import db.genomemap.RDBSettings;

import fs.genomemap.FileSettings;

import javautil.io.IOUtil;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

/**
 * This will be persisted. contains setings for different persistent storage type,e.g.,
 * filesystem,RDBsystem(Relational database),etc.
 * This object will be loaded in the beginning and is responsible for checking
 * the integrity of all resources(filesystem,database,etc..) and report the user.
 * @author Susanta Tewari
 * @version 1.0
 * @updated 14-Nov-2007 7:09:15 PM
 */
public class Preferences implements Serializable {

    /** Field description */
    private static final long serialVersionUID = 42L;

    /** Field description */
    public static final int PERSIS_TYPE_NONE = -1;

    /** Field description */
    private static final int PERSIS_TYPE_FILE = 1;

    /** Field description */
    private static final int PERSIS_TYPE_RDBMS = 2;

    /** Field description */
    private static final String ownPath = "resources/Preferences.obj";

    /** Field description */
    private int persistenceType = PERSIS_TYPE_FILE;

    /** Field description */
    private FileSettings fileSettings = null;

    /** Field description */
    private RDBSettings rdbSettings = null;

    /**
     * Constructs ...
     */
    public Preferences() {}

    public static Preferences load() {

        Preferences preferences = null;

        try {
            preferences = (Preferences) IOUtil.unMarshall(new File(ownPath));
        } catch (IOException | ClassNotFoundException ex) {
            ex.printStackTrace();
        } finally {
            return preferences;
        }
    }

    public boolean commit() {

        boolean result = true;

        try {
            IOUtil.marshall(new File(ownPath), this);
        } catch (Exception ex) {

            ex.printStackTrace();

            result = false;

        } finally {
            return result;
        }
    }

    public static void main(String[] args) {}

    int getPersistenceType() {
        return persistenceType;
    }

    /**
     *
     * @param newVal
     */
    public void setPersistenceType(int newVal) {
        persistenceType = newVal;
    }

    /**
     * true if file system is to be used
     */
    public static boolean useFileSettings() {

        Preferences preferences = Preferences.load();

        if (preferences.getPersistenceType() == Preferences.PERSIS_TYPE_FILE) return true;
        else return false;
    }

    /**
     * true if  RDB (Relational Database) settings is to be used
     */
    public static boolean useRDBSettings() {

        Preferences preferences = Preferences.load();

        if (preferences.getPersistenceType() == Preferences.PERSIS_TYPE_RDBMS) return true;
        else return false;
    }

    public FileSettings getFileSettings() {
        return fileSettings;
    }

    public void setFileSettings(FileSettings fileSettings) {
        this.fileSettings = fileSettings;
    }

    public RDBSettings getRdbSettings() {
        return rdbSettings;
    }

    public void setRdbSettings(RDBSettings rdbSettings) {
        this.rdbSettings = rdbSettings;
    }
}
